home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / su-to-root < prev    next >
Encoding:
Text File  |  2010-09-06  |  3.0 KB  |  118 lines

  1. #!/bin/bash
  2.  
  3. if test -r /etc/su-to-rootrc; then
  4. . /etc/su-to-rootrc
  5. fi
  6.  
  7. if test -r ~/.su-to-rootrc; then
  8. . ~/.su-to-rootrc
  9. fi
  10.  
  11. PRIV=root
  12. COMMAND=
  13. NEEDS=text
  14.  
  15. gettext=$(which gettext 2>/dev/null)
  16.  
  17. transl() {
  18.   txt="$1";
  19.   shift;
  20.   if [ -n "$gettext" ]; then 
  21.     txt="$(gettext su-to-root "$txt")";
  22.   fi
  23.   printf "$txt" "$@"
  24. }
  25.  
  26. eshell() {
  27.    getent passwd $1 | cut -f7 -d:
  28. }
  29.  
  30. usage () {
  31.   transl 'usage: %s [-X] [-p <user>] -c <command>
  32.   -c command: command to execute as a string (mandatory)
  33.   -p <user>: user to switch to (default: root)
  34.   -X: command is a X11 program\n' "$0" >&2
  35.   exit 1
  36. }
  37.  
  38. for i in "$@"; do
  39.    case "$prev" in
  40.      -p)
  41.        PRIV="$i";;
  42.      -c)
  43.        COMMAND="$i";;
  44.      -X) 
  45.        NEEDS="X11";;
  46.    esac
  47.    prev="$i"
  48. done
  49.  
  50. if [ -z "$COMMAND" ] ; then
  51.    usage;
  52. fi
  53.  
  54. euid=$(id -u)
  55. privid=$(id -u $PRIV)
  56. if test "$euid" = "$privid"; then
  57.   sh -c "$COMMAND"
  58. else
  59.   case $NEEDS in
  60.   text)
  61.     if test "$euid" != 0; then
  62.       transl 'About to execute %s.\n' "$COMMAND"
  63.       transl 'This command needs %s privileges to be executed.\n' "$PRIV"
  64.     fi
  65.     PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/X11:/usr/local/sbin:/usr/local/bin
  66.     SHELL=`eshell $PRIV`
  67.     case $SU_TO_ROOT_SU in
  68.       sux)  suname=sux; pwuser="$PRIV"; cmd='sux  -p "$PRIV" -c "$COMMAND"';;
  69.       sudo) suname=sudo;pwuser="$USER"; cmd='sudo -u "$PRIV" sh -c "$COMMAND"';;
  70.       *)    suname=su;  pwuser="$PRIV"; cmd='su   -p "$PRIV" -c "$COMMAND"';;
  71.     esac
  72.     transl 'Using %s...\n' "$suname"
  73.     transl 'Enter %s password at prompt.\n' "$pwuser"
  74.     yesexpr=$(locale yesexpr)
  75.     while ! eval $cmd; do
  76.       transl 'Incorrect password or command failed. Try again? (y/N)'
  77.       read ans
  78.       if echo "$ans" | perl -e "<> =~ /$yesexpr/ and exit(1);"; then
  79.         exit 1
  80.       fi
  81.     done;;
  82.   X11)
  83.     if test -z "$SU_TO_ROOT_X"; then
  84.       if which gksu >/dev/null 2>&1 ; then
  85.         SU_TO_ROOT_X=gksu
  86.         if test "X$KDE_FULL_SESSION" = "Xtrue" ; then
  87.           if which kdesu >/dev/null 2>&1 ; then
  88.             SU_TO_ROOT_X=kdesu
  89.           elif test -x /usr/lib/kde4/libexec/kdesu ; then
  90.             SU_TO_ROOT_X=kde4su
  91.           fi;
  92.         fi;
  93.       elif which kdesu >/dev/null 2>&1 ; then 
  94.         SU_TO_ROOT_X=kdesu
  95.       elif test -x /usr/lib/kde4/libexec/kdesu ; then
  96.         SU_TO_ROOT_X=kde4su
  97.       elif which ktsuss >/dev/null 2>&1 ; then
  98.         SU_TO_ROOT_X=ktsuss
  99.       elif which sux >/dev/null 2>&1 ; then 
  100.         SU_TO_ROOT_X=sux
  101.       else
  102.         SU_TO_ROOT_X=su-to-root
  103.       fi
  104.     fi
  105.     case $SU_TO_ROOT_X in
  106.       gksu) gksu -u "$PRIV" "$COMMAND";;
  107.       kdesu) kdesu -u "$PRIV" "$COMMAND";;
  108.       kde4su) /usr/lib/kde4/libexec/kdesu -u "$PRIV" "$COMMAND";;
  109.       ktsuss) ktsuss -u "$PRIV" "$COMMAND";;
  110.       sux) env SU_TO_ROOT_SU=sux \
  111.         x-terminal-emulator -e su-to-root -p "$PRIV" -c "$COMMAND";;
  112.   # As a last resort, open a new x-terminal-emulator and prompt for the password
  113.   # Do not use -X here!
  114.       *) x-terminal-emulator -e su-to-root -p "$PRIV" -c "$COMMAND";;
  115.     esac;;
  116.   esac
  117. fi
  118.